<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文本加解密工具</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #e4e7f4 100%);
            padding: 20px;
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }

        .container {
            max-width: 500px;
            width: 100%;
            background: rgba(255, 255, 255, 0.7);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            border-radius: 20px;
            box-shadow: 0 8px 32px rgba(31, 38, 135, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.5);
            padding: 30px;
        }

        h1 {
            text-align: center;
            color: #4a4a8c;
            margin-bottom: 25px;
            font-size: 24px;
            font-weight: 600;
            letter-spacing: 0.5px;
        }

        .input-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            color: #5a5a9a;
            font-size: 14px;
            font-weight: 500;
        }

        textarea, input[type="password"] {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid rgba(0, 0, 0, 0.05);
            border-radius: 12px;
            font-size: 14px;
            background: rgba(255, 255, 255, 0.8);
            transition: all 0.3s ease;
            resize: vertical;
            min-height: 45px;
            box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.05),
                        inset -2px -2px 5px rgba(255, 255, 255, 0.8);
        }

        textarea {
            min-height: 120px;
        }

        textarea:focus, input[type="password"]:focus {
            outline: none;
            border-color: rgba(90, 90, 154, 0.3);
            box-shadow: 0 0 0 3px rgba(90, 90, 154, 0.1),
                        inset 2px 2px 5px rgba(0, 0, 0, 0.05),
                        inset -2px -2px 5px rgba(255, 255, 255, 0.8);
        }

        .button-group {
            display: flex;
            gap: 12px;
            margin-bottom: 20px;
        }

        button {
            flex: 1;
            padding: 12px 20px;
            background: rgba(255, 255, 255, 0.8);
            border: 1px solid rgba(255, 255, 255, 0.5);
            border-radius: 12px;
            font-size: 14px;
            cursor: pointer;
            transition: all 0.3s ease;
            font-weight: 500;
            color: #4a4a8c;
            box-shadow: 3px 3px 8px rgba(0, 0, 0, 0.05),
                        -3px -3px 8px rgba(255, 255, 255, 0.8);
        }

        button:hover {
            background: rgba(255, 255, 255, 0.95);
            transform: translateY(-2px);
            box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.08),
                        -5px -5px 15px rgba(255, 255, 255, 0.9);
        }

        button:active {
            transform: translateY(0);
            box-shadow: inset 2px 2px 5px rgba(0, 0, 0, 0.05),
                        inset -2px -2px 5px rgba(255, 255, 255, 0.8);
        }

        .result-area {
            position: relative;
        }

        .clear-btn {
            position: absolute;
            right: 10px;
            top: 10px;
            background: rgba(255, 255, 255, 0.6);
            color: #6c6c9c;
            border: none;
            padding: 6px 12px;
            border-radius: 8px;
            cursor: pointer;
            font-size: 12px;
            font-weight: 500;
            box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.05),
                        -2px -2px 5px rgba(255, 255, 255, 0.8);
            transition: all 0.2s ease;
        }

        .clear-btn:hover {
            background: rgba(255, 255, 255, 0.9);
            transform: translateY(-1px);
        }

        .clear-btn:active {
            transform: translateY(0);
            box-shadow: inset 1px 1px 3px rgba(0, 0, 0, 0.05),
                        inset -1px -1px 3px rgba(255, 255, 255, 0.8);
        }

        @media (max-width: 480px) {
            .container {
                padding: 20px;
            }
            
            h1 {
                font-size: 20px;
            }
            
            button {
                font-size: 13px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>文本加解密工具</h1>
        
        <div class="input-group">
            <label for="textInput">输入文本：</label>
            <textarea id="textInput" placeholder="请输入要加密或解密的文本..."></textarea>
        </div>
        
        <div class="input-group">
            <label for="password">密码（可选）：</label>
            <input type="password" id="password" placeholder="留空则使用默认密钥">
        </div>
        
        <div class="button-group">
            <button onclick="encryptText()">加密</button>
            <button onclick="decryptText()">解密</button>
            <button onclick="clearAll()">清空</button>
        </div>
        
        <div class="input-group result-area">
            <label for="resultOutput">结果：</label>
            <button class="clear-btn" onclick="clearResult()">清除</button>
            <textarea id="resultOutput" placeholder="加密/解密结果将显示在这里..." readonly></textarea>
        </div>
    </div>

    <script>
        // 默认密钥
        const DEFAULT_KEY = 'MyDefaultEncryptionKey123';
        
        // 简单的加密函数（使用异或加密）
        function encrypt(text, key) {
            let result = '';
            for (let i = 0; i < text.length; i++) {
                const charCode = text.charCodeAt(i);
                const keyChar = key.charCodeAt(i % key.length);
                result += String.fromCharCode(charCode ^ keyChar);
            }
            return btoa(unescape(encodeURIComponent(result)));
        }
        
        // 简单的解密函数
        function decrypt(encryptedText, key) {
            try {
                const decoded = decodeURIComponent(escape(atob(encryptedText)));
                let result = '';
                for (let i = 0; i < decoded.length; i++) {
                    const charCode = decoded.charCodeAt(i);
                    const keyChar = key.charCodeAt(i % key.length);
                    result += String.fromCharCode(charCode ^ keyChar);
                }
                return result;
            } catch (e) {
                return '解密失败：无效的密文或密码';
            }
        }
        
        // 加密文本
        function encryptText() {
            const textInput = document.getElementById('textInput');
            const password = document.getElementById('password').value || DEFAULT_KEY;
            const resultOutput = document.getElementById('resultOutput');
            
            if (!textInput.value.trim()) {
                alert('请输入要加密的文本');
                return;
            }
            
            const encrypted = encrypt(textInput.value, password);
            resultOutput.value = encrypted;
        }
        
        // 解密文本
        function decryptText() {
            const textInput = document.getElementById('textInput');
            const password = document.getElementById('password').value || DEFAULT_KEY;
            const resultOutput = document.getElementById('resultOutput');
            
            if (!textInput.value.trim()) {
                alert('请输入要解密的文本');
                return;
            }
            
            const decrypted = decrypt(textInput.value, password);
            resultOutput.value = decrypted;
        }
        
        // 清空所有内容
        function clearAll() {
            document.getElementById('textInput').value = '';
            document.getElementById('password').value = '';
            document.getElementById('resultOutput').value = '';
        }
        
        // 清空结果
        function clearResult() {
            document.getElementById('resultOutput').value = '';
        }
        
        // 支持回车键操作
        document.getElementById('textInput').addEventListener('keydown', function(e) {
            if (e.ctrlKey && e.key === 'Enter') {
                encryptText();
            }
        });
        
        document.getElementById('resultOutput').addEventListener('keydown', function(e) {
            if (e.ctrlKey && e.key === 'Enter') {
                decryptText();
            }
        });
    </script>
</body>
</html>